﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.Data;
using UI.ComplexEntities;
using UI.Helpers;

namespace UI.Controllers
{
    public class AccountController : Controller
    {
        private SPORTSALEEntities db = new SPORTSALEEntities();

        //
        // GET: /Account/Details/5

        public ActionResult Details(int id = 0)
        {
            Persona persona = db.Persona.Single(p => p.Id == id);
            if (persona == null)
            {
                return HttpNotFound();
            }
            return View(persona);
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Usuarios u)
        {
            try
            {
                Usuarios current = db.Usuarios.Where(x => x.Username == u.Username).First();
                MySession.Current.UserId = current.Id;
                MySession.Current.Rol = current.Roles.Nombre;
            }
            catch (Exception)
            {
                this.ModelState.AddModelError("UserName", "El nombre de usuario o contraseña son invalidos");
                return View();
            }

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Account/Create

        [HttpPost]
        public ActionResult Create(Persona persona, FormCollection col)
        {
            string username = col["username"];
            string pass = col["pass"];
            string email = col["email"];

            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(pass) || String.IsNullOrEmpty(email))
            {
                ModelState.AddModelError("ValidationSummary1", "Por favor complete los datos de su cuenta de usuario");
                return View(persona);
            }

            if (ModelState.IsValid)
            {
                db.Persona.AddObject(persona);
                Usuarios u = new Usuarios();
                u.Persona = persona;
                u.Roles = db.Roles.Where(x => x.Id == 2).First();
                u.Username = username;
                u.Password = pass;
                u.Email = email;

                db.Usuarios.AddObject(u);

                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }

            return View(persona);
        }

        //
        // GET: /Account/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Persona persona = db.Persona.Single(p => p.Id == id);
            if (persona == null)
            {
                return HttpNotFound();
            }
            return View(persona);
        }

        //
        // POST: /Account/Edit/5

        [HttpPost]
        public ActionResult Edit(Persona persona)
        {
            if (ModelState.IsValid)
            {
                db.Persona.Attach(persona);
                db.ObjectStateManager.ChangeObjectState(persona, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(persona);
        }

        //
        // GET: /Account/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Persona persona = db.Persona.Single(p => p.Id == id);
            if (persona == null)
            {
                return HttpNotFound();
            }
            return View(persona);
        }

        //
        // POST: /Account/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Persona persona = db.Persona.Single(p => p.Id == id);
            db.Persona.DeleteObject(persona);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult LogOff()
        {
            Session.Abandon();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Manage(int userId)
        {
            return View(db.Usuarios.Where(x => x.Id == userId).First());
        }
    }
}